package com.slz3.business.module.configManagement.conZoneType.domain;

import com.slz3.business.frame.enums.AuditFlag;
import com.slz3.business.frame.enums.UsingFlag;
import com.slz3.business.frame.enums.YNFlag;
import com.slz3.business.frame.util.LocalDT;
import com.slz3.business.frame.util.UUIDUtils;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.configManagement.conZoneType.enums.ConZoneTypePositionEnum;
import org.springframework.util.MultiValueMap;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

public class ConZoneTypeFactory {

    /**
     * 初始化基础属性值
     */
    public static ConZoneType initBaseFields(ConZoneType conZoneType) {
        if (conZoneType.getUuid() == null) {
            conZoneType.setUuid(UUIDUtils.generateUUID());
            conZoneType.setCreateTime(LocalDateTime.now());
            if (VE.isEmpty(conZoneType.getSerAudit())) {
                conZoneType.setSerAudit(AuditFlag.WAIT_SUBMIT);
            }
            if (VE.isEmpty(conZoneType.getSerUsing())) {
                conZoneType.setSerUsing(UsingFlag.ENABLE);
            }
        } else {
            conZoneType.setLastModifyTime(LocalDateTime.now());
        }
        return conZoneType;
    }

    /**
     * 初始化基础属性值
     */
    public static ConZoneType initBaseFields() {
        return initBaseFields(newTemStyle());
    }

    public static ConZoneType newTemStyle() {
        return new ConZoneType();
    }

    public static ConZoneTypeCondition newConZoneTypeCondition(MultiValueMap<String, String> params) {

        ConZoneTypeCondition condition = new ConZoneTypeCondition();

        String createTimeOUTSET = params.getFirst("createTimeOUTSET");
        String createTimeFINISH = params.getFirst("createTimeFINISH");
        String lastModifyTimeOUTSET = params.getFirst("lastModifyTimeOUTSET");
        String lastModifyTimeFINISH = params.getFirst("lastModifyTimeFINISH");
        String fkUpperZoneIsNull = params.getFirst("fkUpperZoneIsNull");
        String fkZoneTemplate = params.getFirst("fkZoneTemplate");
        String fkZoneType = params.getFirst("fkZoneType");

        List<String> serAuditList = params.get("serAuditList"); // AuditFlag
        List<String> fkUpperZoneList = params.get("fkUpperZoneList"); // UUID
        List<String> fkZoneTemplateList = params.get("fkZoneTemplateList"); // UUID
        List<String> enuPositionList = params.get("enuPositionList"); // ConZoneTypePositionEnum

        condition.setCreateTimeOUTSET(VE.isEmpty(createTimeOUTSET) ? null : LocalDT.parseStringToDateTime(createTimeOUTSET, ConZoneTypeCondition.CREATE_TIME_OF_PATTERN));
        condition.setCreateTimeFINISH(VE.isEmpty(createTimeFINISH) ? null : LocalDT.parseStringToDateTime(createTimeFINISH, ConZoneTypeCondition.CREATE_TIME_OF_PATTERN));
        condition.setLastModifyTimeOUTSET(VE.isEmpty(lastModifyTimeOUTSET) ? null : LocalDT.parseStringToDateTime(lastModifyTimeOUTSET, ConZoneTypeCondition.CREATE_TIME_OF_PATTERN));
        condition.setLastModifyTimeFINISH(VE.isEmpty(lastModifyTimeFINISH) ? null : LocalDT.parseStringToDateTime(lastModifyTimeFINISH, ConZoneTypeCondition.CREATE_TIME_OF_PATTERN));
        condition.setFkUpperZoneIsNull(VE.isEmpty(fkUpperZoneIsNull) ? null : YNFlag.valueOf(fkUpperZoneIsNull));
        condition.setFkZoneTemplate(VE.isEmpty(fkZoneTemplate) ? null : UUID.fromString(fkZoneTemplate));
        condition.setUuid(VE.isEmpty(fkZoneType) ? null : UUID.fromString(fkZoneType));

        condition.setSerAuditList(VE.isEmpty(serAuditList) ? null : serAuditList.stream().map(item -> AuditFlag.valueOf(item)).collect(Collectors.toList()));
        condition.setFkUpperZoneList(VE.isEmpty(fkUpperZoneList) ? null : fkUpperZoneList.stream().map(item -> UUID.fromString(item)).collect(Collectors.toList()));
        condition.setFkZoneTemplateList(VE.isEmpty(fkZoneTemplateList) ? null : fkZoneTemplateList.stream().map(item -> UUID.fromString(item)).collect(Collectors.toList()));
        condition.setEnuPositionList(VE.isEmpty(enuPositionList) ? null : enuPositionList.stream().map(item -> ConZoneTypePositionEnum.valueOf(item)).collect(Collectors.toList()));

        return condition;

    }

}
